home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / nethandler.lha / NetHandler / handler / main.c < prev    next >
C/C++ Source or Header  |  1989-09-16  |  14KB  |  350 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1987, 1988 The Software Distillery.  All Rights  */
  3. /* |. o.| || Reserved.  This program may not be distributed without the    */
  4. /* | .  | || permission of the authors:                            BBS:    */
  5. /* | o  | ||   John Toebes     Doug Walker    Dave Baker                   */
  6. /* |  . |//                                                                */
  7. /* ======                                                                  */
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include "handler.h"
  11.  
  12. /******************************************************************************/
  13. /******************************************************************************/
  14. /********************* Dispatch table to handle all packets *******************/
  15. /******************************************************************************/
  16. /******************************************************************************/
  17. #define BP1 1
  18. #define BP2 2
  19. #define BP3 4
  20. #define BP4 8
  21.  
  22. typedef void (*ifuncp)(GLOBAL, struct DosPacket *);
  23. struct LookupTable
  24.    {
  25.    ifuncp subr;
  26.    int flags;
  27.    };
  28.  
  29. #define LO_FIRST 0
  30. #define LO_LAST  34
  31. struct LookupTable lowork[LO_LAST+1] = {
  32.    { NULL,              0  | 0  | 0  | 0   }, /*  0 - ACTION_NIL           */
  33.    { NULL,              0  | 0  | 0  | 0   }, /*  1 - Unknown              */
  34.    { NULL,              BP1| BP2| BP3| 0   }, /*  2 - ACTION_GET_BLOCK     */
  35.    { NULL,              0  | BP2| BP3| 0   }, /*  3 - Unknown              */
  36.    { NULL,              BP1| BP2| BP3| 0   }, /*  4 - ACTION_SET_MAP       */
  37.    { ActDie,            0  | 0  | 0  | 0   }, /*  5 - ACTION_DIE           */
  38.    { NULL,              0  | 0  | 0  | 0   }, /*  6 - ACTION_EVENT         */
  39.    { ActCurentVol,      BP1| 0  | 0  | 0   }, /*  7 - ACTION_CURRENT_VOLUME*/
  40.    { ActLock,           BP1| BP2| 0  | 0   }, /*  8 - ACTION_LOCATE_OBJECT */
  41.    { ActRenameDisk,     BP1| BP2| 0  | 0   }, /*  9 - ACTION_RENAME_DISK   */
  42.    { NULL,              0  | 0  | 0  | 0   }, /* 10 - Unknown              */
  43.    { NULL,              0  | 0  | 0  | 0   }, /* 11 - Unknown              */
  44.    { NULL,              0  | 0  | 0  | 0   }, /* 12 - Unknown              */
  45.    { NULL,              0  | 0  | 0  | 0   }, /* 13 - Unknown              */
  46.    { NULL,              0  | 0  | 0  | 0   }, /* 14 - Unknown              */
  47.    { ActUnLock,         BP1| 0  | 0  | 0   }, /* 15 - ACTION_FREE_LOCK     */
  48.    { ActDelete,         BP1| BP2| 0  | 0   }, /* 16 - ACTION_DELETE_OBJECT */
  49.    { ActRename,         BP1| BP2| BP3| BP4 }, /* 17 - ACTION_RENAME_OBJECT */
  50.    { NULL,              0  | 0  | 0  | 0   }, /* 18 - ACTION_MORE_CACHE    */
  51.    { ActDupLock,        BP1| 0  | 0  | 0   }, /* 19 - ACTION_COPY_DIR      */
  52.    { NULL,              0  | 0  | 0  | 0   }, /* 20 - ACTION_WAIT_CHAR     */
  53.    { ActSetProtection,  0  | BP2| BP3| 0   }, /* 21 - ACTION_SET_PROTECT   */
  54.    { ActCreateDir,      BP1| BP2| 0  | 0   }, /* 22 - ACTION_CREATE_DIR    */
  55.    { ActExamine,        BP1| BP2| 0  | 0   }, /* 23 - ACTION_EXAMINE_OBJECT*/
  56.    { ActExNext,         BP1| BP2| 0  | 0   }, /* 24 - ACTION_EXAMINE_NEXT  */
  57.    { ActDiskInfo,       BP1| 0  | 0  | 0   }, /* 25 - ACTION_DISK_INFO     */
  58.    { ActInfo,           BP1| BP2| 0  | 0   }, /* 26 - ACTION_INFO          */
  59.    { ActFlush,          0  | 0  | 0  | 0   }, /* 27 - ACTION_FLUSH         */
  60.    { ActSetComment,     0  | BP2| BP3| BP4 }, /* 28 - ACTION_SET_COMMENT   */
  61.    { ActParent,         BP1| 0  | 0  | 0   }, /* 29 - ACTION_PARENT        */
  62.    { NULL,              BP1| 0  | 0  | 0   }, /* 30 - ACTION_TIMER         */
  63.    { ActInhibit,        0  | 0  | 0  | 0   }, /* 31 - ACTION_INHIBIT       */
  64.    { NULL,              BP1| 0  | 0  | 0   }, /* 32 - ACTION_DISK_TYPE     */
  65.    { NULL,              0  | 0  | 0  | 0   }, /* 33 - ACTION_DISK_CHANGE   */
  66.    { ActSetFileDate,    0  | 0  | 0  | 0   }  /* 34 - ACTION_SET_FILE_DATE */
  67.       };
  68.  
  69. #define HI_FIRST 1004
  70. #define HI_LAST  1008
  71. struct LookupTable hiwork[5] = {
  72.    { ActFindwrite,      BP1| BP2| BP3| 0   }, /* ACTION_FIND_WRITE  - 1004 */
  73.    { ActFindwrite,      BP1| BP2| BP3| 0   }, /* ACTION_FIND_INPUT  - 1005 */
  74.    { ActFindwrite,      BP1| BP2| BP3| 0   }, /* ACTION_FIND_OUTPUT - 1006 */
  75.    { ActEnd,            0  | 0  | 0  | 0   }, /* ACTION_END         - 1007 */
  76.    { ActSeek,           0  | 0  | 0  | 0   }  /* ACTION_SEEK        - 1008 */
  77.    };
  78.  
  79. #define USER_FIRST 2010
  80. #define USER_LAST  2012
  81. struct LookupTable userwork[3] = {
  82.    { ActSetDebug,       0  | 0  | 0  | 0   }, /* ACTION_HANDLER_DEBUG 2010 */
  83.    { NULL,              BP1| 0  | 0  | 0   }, /* ACTION_SET_TRANS_TYPE2011 */
  84.    { ActNetHello,       BP1| 0  | 0  | 0   }, /* ACTION_NETWORK_HELLO 2012 */
  85.    };
  86.  
  87. struct DosLibrary *DOSBase;
  88.  
  89. void _main(x)
  90. char *x;
  91. {
  92.    struct DosPacket   *mypkt;      /* a pointer to the dos packet sent       */
  93.    int                action;
  94.    ifuncp             subr;
  95.    int                flags;
  96.    struct global      global;
  97.  
  98.    DOSBase = (struct DosLibrary *)OpenLibrary(DOSNAME,0);
  99.  
  100.    /* Initialize our global data structure */
  101.    memset((char *)&global, 0, sizeof(struct global));
  102.    global.n.self   = (struct Process *) FindTask(0L);  /* find myself      */
  103.    global.n.run    = 1;
  104.    global.n.port   = &(global.n.self->pr_MsgPort);
  105.                     /* install our taskid ...   */
  106.  
  107.  
  108.    /* Initialize the intuitext structures for the requesters we might have   */
  109.    /* to display                                                             */
  110.    /* Because we have no scruples we can cheat and do this with a couple of  */
  111.    /* long word assignments.  We leave the acual C code commented out here   */
  112.    /* so that if this structure ever changed we will still be able to work   */
  113. #if 0
  114.    global.n.line1.FrontPen = global.n.line1.BackPen = -1;
  115.    global.n.line1.DrawMode = JAM1;
  116.    global.n.line1.LeftEdge = global.n.line1.TopEdge = 4;
  117.    global.n.line2 = global.n.line1;
  118.    global.n.line3 = global.n.line1;
  119.    global.n.retrytxt = global.n.line1;
  120.    global.n.canceltxt = global.n.line1;
  121. #else
  122.    *(long *)&global.n.line1.FrontPen     = 0x00010000L | (JAM1<<8);
  123.    *(long *)&global.n.line1.LeftEdge     = 0x00040004L;  /* 4,4  */
  124.    *(long *)&global.n.line2.FrontPen     = 0x00010000L | (JAM1<<8);
  125.    *(long *)&global.n.line2.LeftEdge     = 0x0004000EL;  /* 4,14 */
  126.    *(long *)&global.n.line3.FrontPen     = 0x00010000L | (JAM1<<8);
  127.    *(long *)&global.n.line3.LeftEdge     = 0x00040018L;  /* 4,24 */
  128.    *(long *)&global.n.retrytxt.FrontPen  = 0x00010000L | (JAM1<<8);
  129.    *(long *)&global.n.retrytxt.LeftEdge  = 0x00040004L;
  130.    *(long *)&global.n.canceltxt.FrontPen = 0x00010000L | (JAM1<<8);
  131.    *(long *)&global.n.canceltxt.LeftEdge = 0x00040004L;
  132. #endif
  133.    global.n.retrytxt.IText = "Retry";
  134.    global.n.canceltxt.IText = "Cancel";
  135.  
  136.    /* since we were started as a non-BCPL module we get sent the parameter */
  137.    /* packet (ie. parameter packet not in D1) */
  138.  
  139.    mypkt = taskwait(&global);  /* wait for parameter packet */
  140.  
  141.    global.n.devname  = (((char *)BADDR(mypkt->dp_Arg1))+1);  /* BSTR name */
  142.  
  143.    /* get pointer to our device node */
  144.    global.node = (struct DeviceNode *) BADDR(mypkt->dp_Arg3);
  145.    global.node->dn_Task = global.n.port;
  146.  
  147.    InitDevice(&global);
  148.  
  149.    Mount(&global, NULL);
  150.  
  151. /*   OpenTimer(&global); */
  152.  
  153.    mypkt->dp_Res1 = DOS_TRUE;
  154.    retpkt(&global, mypkt);
  155.  
  156. /*   PostTimerReq(&global); */
  157.  
  158.    while(global.n.run)   /* start of the real work */
  159.    {
  160.       BUG(("Waiting for packet. . ."));
  161.       mypkt = taskwait(&global);  /* wait for a packet */
  162.  
  163.       action = mypkt->dp_Type;
  164.  
  165.       BUG(("action #%ld\n", action));
  166.  
  167.       switch (action)
  168.       {
  169.  
  170.          case ACTION_NETWORK_KLUDGE:
  171.             subr = ActNetKludge;
  172.             flags = (BP1 | BP2);
  173.             break;
  174.  
  175.          case ACTION_READ:
  176.             subr = ActRead;
  177.             flags = 0;
  178.             break;
  179.          case ACTION_WRITE:
  180.             subr = ActWrite;
  181.             flags = 0;
  182.             break;
  183.  
  184.          case ACTION_SET_RAW_MODE:
  185.             subr = NULL;
  186.             flags = 0;
  187.             break;
  188.  
  189.          case ACTION_FIND_WRITE:  /* 1004 */
  190.          case ACTION_FIND_INPUT:  /* 1005 */
  191.          case ACTION_FIND_OUTPUT: /* 1006 */
  192.          case ACTION_END:         /* 1007 */
  193.          case ACTION_SEEK:        /* 1008 */
  194.             subr = hiwork[action-HI_FIRST].subr;
  195.             flags = hiwork[action-HI_FIRST].flags;
  196.             break;
  197.  
  198.          case ACTION_HANDLER_DEBUG:  /* 2010 */
  199.          case ACTION_SET_TRANS_TYPE: /* 2011 */
  200.          case ACTION_NETWORK_HELLO:  /* 2012 */
  201.             subr = userwork[action-USER_FIRST].subr;
  202.             flags = userwork[action-USER_FIRST].flags;
  203.             break;
  204.  
  205.          default:            
  206.             if ((action >= LO_FIRST) && (action <= LO_LAST))
  207.             {
  208.                subr = lowork[action-LO_FIRST].subr;
  209.                flags = lowork[action-LO_FIRST].flags;
  210.             }
  211.             else
  212.                subr = NULL;
  213.       }
  214.  
  215.       mypkt->dp_Res1 = DOS_FALSE;
  216.       mypkt->dp_Res2 = ERROR_ACTION_NOT_KNOWN;
  217.  
  218.       if (subr != NULL)
  219.       {
  220.          global.n.reply = 1;
  221.          if (flags & BP1)
  222.             mypkt->dp_Arg1 <<= 2;
  223.          if (flags & BP2)
  224.             mypkt->dp_Arg2 <<= 2;
  225.          if (flags & BP3)
  226.             mypkt->dp_Arg3 <<= 2;
  227.          if (flags & BP4)
  228.             mypkt->dp_Arg4 <<= 2;
  229.  
  230.          (*subr)(&global, mypkt);
  231.  
  232.       }
  233. #if DEBUG
  234.       else
  235.       {
  236.          BUG(("Unknown packet type %ld\n",mypkt->dp_Type));
  237.       }
  238. #endif
  239.  
  240.       /* Now return the packet to them */
  241.       if (global.n.reply)
  242.          retpkt(&global, mypkt);
  243.   
  244.       BUG(("-----\n"));
  245.    }
  246.  
  247.    /* do our final cleanup */
  248.    global.node->dn_Task = FALSE; /* zero the taskid field of device node */
  249.    global.node->dn_SegList = 0;  /* make us be gone */
  250.  
  251.    DisMount(&global);
  252.    TermDevice(&global);
  253.  
  254.    BUGTERM()
  255. }
  256.  
  257. void ActSetDebug(global, pkt)  /* ACTION_HANDLER_DEBUG */
  258. GLOBAL global;
  259. struct DosPacket *pkt;
  260. /* DP_Arg1 - LONG type/flags 0=nodebug                  */
  261. /* DP_Arg2 - BPTR FileHandle to do debugging to or NULL */
  262. /* DP_Res1 - BPTR old filehandle                        */
  263. {
  264.    /********************************************************************/
  265.    /*                                                                  */
  266.    /* Several possibilities:                                           */
  267.    /* 1. Arg1 is 0:                                                    */
  268.    /*       All debugging is turned off.  Arg2 is not looked at.       */
  269.    /*       Res1 is DOS_TRUE, Res2 is a BPTR to the old filehandle.    */
  270.    /*                                                                  */
  271.    /* 2. Arg1 is a special handler-defined code:                       */
  272.    /*       If the second bit from the top is ON, the code is a special*/
  273.    /*       debugging command to the handler.  If the handler knows the*/
  274.    /*       command, it returns DOS_TRUE in Res1.  If it doesn't, it   */
  275.    /*       returns DOS_FALSE in Res1 and Res2.                        */
  276.    /*                                                                  */
  277.    /* 3. Neither of the above:                                         */
  278.    /*       Arg2 contains a BPTR to a FileHandle to send debugging to. */
  279.    /*       Note that if this is NULL, debugging will be turned off.   */
  280.    /*       Res1 contains DOS_TRUE, Res2 contains a BPTR to the old    */
  281.    /*       debugging FileHandle, which may of course be NULL if no    */
  282.    /*       debugging was on before.                                   */
  283.    /*                                                                  */
  284.    /********************************************************************/
  285.  
  286. #define DEBUG_SPECIAL 0x40000000   /* Mask for handler-defined dbg type*/
  287. #define DEBUG_SERVER  0x20000000   /* Mask indicating server command   */
  288. #define DEBUG_WAIT    0x40000001   /* Wait for debugger to catch us    */
  289. #define DEBUG_INFO    0x40000002   /* Send transmit info to msgport in */
  290.                                    /* dp_Arg2                          */
  291.  
  292.    struct NetNode *netnode;
  293.    extern BPTR debuglog;
  294.  
  295.    pkt->dp_Res1 = DOS_TRUE;
  296.    if(pkt->dp_Arg1 & DEBUG_SPECIAL)
  297.    {
  298.       BUG(("ActSetDebug: Special debug packet %lx\n", pkt->dp_Arg1))
  299.       if(pkt->dp_Arg1 == DEBUG_WAIT)
  300.       {
  301.          pkt->dp_Res2 = NULL;
  302.          cprwait(global);
  303.       }
  304.       else if(pkt->dp_Arg1 == DEBUG_INFO)
  305.       {
  306.          global->n.infoport = (struct MsgPort *)BADDR(pkt->dp_Arg2);
  307.          global->n.ntirec.m.mn_Node.ln_Type =
  308.          global->n.ntitrans.m.mn_Node.ln_Type = NT_MESSAGE;
  309.          if(!global->n.ntirec.m.mn_ReplyPort)
  310.             global->n.ntirec.m.mn_ReplyPort =
  311.             global->n.ntitrans.m.mn_ReplyPort = CreatePort(NULL,0);
  312.          global->n.inf_rec = global->n.inf_trans = 0L;
  313.       }
  314.       else if(pkt->dp_Arg1 & DEBUG_SERVER)
  315.       {
  316.          global->RP.Type = ACTION_HANDLER_DEBUG;
  317.          global->RP.Arg1 = pkt->dp_Arg1 & ~(DEBUG_SERVER|DEBUG_SPECIAL);
  318.          BUG(("Remote debugging code %lx\n", global->RP.Arg1))
  319.          for(netnode=global->netchain.next;
  320.              netnode;
  321.              netnode=netnode->next)
  322.          {
  323.             if(netnode->status == NODE_UP &&
  324.                netnode->RootLock.RDevice)
  325.             {
  326.                RemotePacket(global, &netnode->RootLock);
  327.             }
  328.          }
  329.       }
  330.       else
  331.       {
  332.          pkt->dp_Res1 = 
  333.          pkt->dp_Res2 = DOS_FALSE;
  334.       }
  335.    }
  336. #if DEBUG
  337.    else if(pkt->dp_Arg1)
  338.       pkt->dp_Res2 = initdebug((BPTR)pkt->dp_Arg2);
  339.    else
  340.    {
  341.       pkt->dp_Res2 = debuglog;
  342.       debuglog = NULL;
  343.    }
  344. #endif   
  345. }
  346.  
  347.  
  348.  
  349.  
  350.